/*
 * SIT_Status.c : status bar at bottom of window
 *
 * Written by T.Pierron, Nov 1, 2009.
 */

#include "SIT_P.h"
#include "graphics.h"
#include <stdio.h>

	TagList StatusClass[] = {
		{ SIT_SuperClass, NULL,         ___, SIT_INT, (ULONG) WidgetClass },
		{ SIT_Styles,     "styles",     __G, SIT_INT, OFFSET(SIT_Status, st_Styles) },
		{ SIT_LabelSize,  "labelSize",  _SG, SIT_STR, OFFSET(SIT_Status, st_LabelSize) },
		{ SIT_StatusPart, "statusPart", __G, SIT_INT, OFFSET(SIT_Status, st_StatusPart) },
		{ SIT_TagEnd }
	};

void SIT_SetStatusPart(SIT_Widget w)
{
	SIT_Status s = (SIT_Status) w;
	HWND   parent = SIT_GetContainer(w);
	int    nbpart = 0;
	int *  parts  = NULL;
	GC     gc     = NULL;
	ULONG  width, i, rem;
	STRPTR p;
	RECT   rect;

	GetClientRect(parent, &rect);

	if (IsDef(s->st_LabelSize))
	{
		nbpart = StrCount(s->st_LabelSize, '\t') + 1;
		parts  = alloca(nbpart * sizeof *parts);
		width  = rect.right;

		memset(parts, 0, nbpart * sizeof *parts);

		for (p = s->st_LabelSize, rem = i = 0; *p; i ++)
		{
			if (*p == '{')
			{
				if (gc == NULL) gc = GFX_GetGC(w);
				do {
					STRPTR end;
					for (end = ++ p; *end && *end != '}' && *end != '|'; end ++);
					int len = GFX_TextLength(gc, p, end - p) + 10; p = end;
					if (parts[i] < len)
						parts[i] = len;
				} while (*p && *p != '}');
			} else {
				parts[i] = strtoul(p, &p, 10);

				if (*p == '%') parts[i] = - parts[i];
			}

			if (parts[i] > 0)
				width -= parts[i];
			else
				rem ++;

			while (*p && *p != '\t') p ++;
			if (*p) p ++;
		}
		for (i = 0; i < nbpart; i ++)
		{
			if (parts[i] <= 0)
			{
				parts[i] = width * (-parts[i]) / (rem * 100);
				width -= parts[i];
				rem --;
			}
			if (i > 0) parts[i] += parts[i - 1];
		}
		SendMessage(w->sw_Handle, SB_SETPARTS, nbpart, (LPARAM) parts);
		if (gc) GFX_Free(gc);
	}
	else SendMessage(w->sw_Handle, SB_SETPARTS, 0, 0);
}

static int SIT_SetStatusValues(SIT_Widget w, APTR cd, APTR user_data)
{
	SIT_SetWidgetValue(w, cd, user_data);

	if (((TagList *)cd)->tl_TagID == SIT_LabelSize && w->sw_Handle)
		SIT_SetStatusPart(w);
	return 1;
}

BOOL SIT_CreateStatus(SIT_Widget w, va_list args)
{
	SIT_Status status = (SIT_Status) w;
	HWND       hwnd;
	int        style = 0;

	/* Default values */
	w->sw_SetValue = SIT_SetStatusValues;
	SIT_ParseTags(w, args, w->sw_Class = StatusClass);

	switch (status->st_Styles) {
	case SITV_HasResize: style |= SBARS_SIZEGRIP; break;
	}

	if (w->sw_Visible) style |= WS_VISIBLE;

	/* Create Win32 control, pos and dim will be overriden by control */
	hwnd = w->sw_Handle = CreateWindowEx(0, STATUSCLASSNAME, NULL, WS_CHILD | style,
		0, 0, 0, 0, SIT_GetContainer(w), (HMENU) MAKE_ID(w), instance, NULL);

	if (hwnd == NULL) return FALSE;
	SIT_SetDefaultFont(w);

	/* Keep track of class inside callback */
	SetProp(hwnd, sit_class, (HANDLE) w);

	if (status->st_LabelSize)
		SIT_SetStatusPart(w);

	return TRUE;
}

DLLIMP void SIT_SetStatusText(SIT_Widget w, STRPTR text, int part)
{
	if (w == NULL) return;
	if (w->sw_Type == SIT_DIALOG && (w = ((SIT_Dialog)w)->sd_Status) == NULL) return;
	if (text != SITV_OwnerDrawn)
	{
		LPWSTR msg = L"";
		if (text) UTF8ToUTF16(text, msg);
		SendMessage(w->sw_Handle, SB_SETTEXT, part, (LPARAM) msg);
	}
	else SendMessage(w->sw_Handle, SB_SETTEXT, part | SBT_OWNERDRAW, 0);
}

